/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package SearchController;

import SearchModels.Logic;
import SearchViews.SearchBar;
import SearchViews.Widget;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author Israel Agyeman-Premp
 */
public class SearchBarController {
    MouseListener mouseListener ;
    MouseMotionListener mouseMotionListener;
    SearchBar s;
    Widget w;
    Logic l;
    
    public SearchBarController(SearchBar s, Widget w, Logic l){
        this.s = s;
        this.w = w;
        this.l = l;
        
        w.setVisible(true);
    }
    
    public void control(){
         mouseListener = new MouseListener(){

            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getSource() == w.getMain()){
                    s.setVisible(true);                    
                    if(e.getClickCount() == 2){
                    String text = s.getTextField().getText();
                    System.out.println(text);
                    l.search(text);                    
                    s.dispose();
                    }
                    else if(e.getClickCount() == 3){
                    String text = s.getTextField().getText();
                    System.out.println(text);
                    l.searchYoutube(text);                    
                    s.dispose();
                    }else if (SwingUtilities.isRightMouseButton(e)) {
                        //w.hide();
                        s.hide();
                    }
                }
            }
            

            @Override
            public void mousePressed(MouseEvent e) {  }

            @Override
            public void mouseReleased(MouseEvent e) {  }

            @Override
            public void mouseEntered(MouseEvent e) {  }

            @Override
            public void mouseExited(MouseEvent e) {  }
        };
         
         w.getMain().addMouseListener(mouseListener);
         
         mouseMotionListener = new MouseMotionListener(){

             @Override
             public void mouseDragged(MouseEvent e) {
                 if(e.getSource()==w.getMain()){
                     s.setLocation(w.xPos()-350,w.yPos()+15);
                 }
             }

             @Override
             public void mouseMoved(MouseEvent e) {
                
             }
         };
         
         w.getMain().addMouseMotionListener(mouseMotionListener);
         
         
    }
    
    
}
